package com.ltmonitor.web.controller;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

import com.ltmonitor.entity.AlarmConfig;
import com.ltmonitor.entity.DriverInfo;
import com.ltmonitor.entity.VehicleStateIcon;
import com.ltmonitor.service.IVehicleStateIconConfigService;
import com.ltmonitor.web.util.JsonMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 配置报警类型是否启用，报警声音是否启用。
 * @author Administrator
 *
 */

@Controller
@RequestMapping("/vehicleStateIconConfig")
public class VehicleStateIconConfigAction extends GenericAction {
	@Autowired
	private IVehicleStateIconConfigService vehicleStateIconConfigService;

	protected Class getEntityClass() {
		return VehicleStateIcon.class;
	}


	@RequestMapping("/list")
	public String list(Model m) {
		List<VehicleStateIcon> vehicleStateIcons = vehicleStateIconConfigService.getVehicleStateIconList();
		
		for(VehicleStateIcon a: vehicleStateIcons)
		{
			String vehicleType = super.convert(a.getVehicleType(), "VehicleType");
			a.setVehicleType(vehicleType);
			String vehicleStatus = super.convert(a.getVehicleStatus(), "VehicleStatus");
			a.setVehicleStatus(vehicleStatus);
		}

		m.addAttribute("vehicleStateIcons", vehicleStateIcons);
		return "/vehicleStateIconConfig/vehicleStateIconList";

	}



	@RequestMapping("/viewConfig.action")
	public ModelAndView view(Model model,
							 @RequestParam(value = "entityId", defaultValue="0") long id) {

		ModelAndView m = new ModelAndView();
		m.setViewName("/vehicleStateIconConfig/editVehicleStateIconConfig");
		try {
			if (id >0) {
				VehicleStateIcon t = vehicleStateIconConfigService.load(id);
				if(t == null)
				{

					m.addObject("entity", new DriverInfo());
				}else
					m.addObject("entity", t);
			}else
			{
				m.addObject("entity", new VehicleStateIcon());
			}

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			m.addObject("发生错误:"+ex.getMessage());
		}

		return m;
	}

	/**
	 * 新增后的保存操作
	 * 
	 * @return
	 */
	@RequestMapping("/save")
	public String save(VehicleStateIcon d, Model m) throws Exception {

		try {

			//一个车类型只能有一个默认的图标
			if(d.isDefaultVehicleType())
			{
				String hsql = "from VehicleStateIcon where vehicleStatus = ? and defaultVehicleType = ? and entityId <> ? ";
				List ls = this.vehicleStateIconConfigService.query(hsql, new Object[]{d.getVehicleStatus(),true,d.getEntityId()});
				for(Object obj : ls)
				{
					VehicleStateIcon dr = (VehicleStateIcon)obj;
					dr.setDefaultVehicleType(false);
					this.vehicleStateIconConfigService.saveOrUpdate(dr);
				}
			}
			this.vehicleStateIconConfigService.saveOrUpdate(d);

			m.addAttribute("message", "保存成功");
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			m.addAttribute("message", ex.getMessage());
		}

		m.addAttribute("entity", d);
		return "/vehicleStateIconConfig/editVehicleStateIconConfig";
	}

	/**
	 * 假删除
	 * @param entityId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete.action")
	public JsonMessage delete(long entityId) {
		try {
			this.vehicleStateIconConfigService.deleteById(entityId);
			return jsonSuccess("记录已被成功刪除!");

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return jsonFail(ex.getMessage());
		}

	}
}
